<?php

namespace app\common\model\user;

use app\common\model\level\Config;
use app\common\model\MoneyLog;
use app\common\model\User;
use fast\Random;
use think\Model;


class Level extends Model
{





    // 表名
    protected $name = 'user_level';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'paytype_text',
        'paytime_text',
        'pay_status_text'
    ];



    public function getPaytypeList()
    {
        return ['1' => __('Paytype 1')];
    }

    public function getPayStatusList()
    {
        return ['0' => __('Pay_status 0'), '10' => __('Pay_status 10'), '-1' => __('Pay_status -1')];
    }


    public function getPaytypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytype']) ? $data['paytype'] : '');
        $list = $this->getPaytypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function config()
    {
        return $this->belongsTo('app\common\model\level\Config', 'vip_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    static function open_vip($order_sn, $transaction_id)
    {
        $order = self::get(['orderid' => $order_sn, 'pay_status' => '0']);
        if ($order) {
            $config = Config::get(1);
            $order->pay_status = '10';
            $order->paytime = time();
            $order->transaction_id = $transaction_id;
            $order->save();
            $user = User::get(['id' => $order->user_id, 'level' => 0]);
            if ($user) {
                $user->level = 1;
                $user->vip_sn =  vip_sn('user', 'vip_sn');
                $user->validitytime = strtotime("+" . $config->day . " day");
                $user->save();
                $model = new Credit;
                $jsonArray = json_decode($config->json, true);
                foreach ($jsonArray as $v) {
                    $data[] = [
                        'user_id' => $order->user_id,
                        'goods_id' => $v['id'],
                        'total' => $v['content'],
                        'num' => $v['content'],
                    ];
                }
                $model->saveAll($data);
                MoneyLog::vipcommission($order->user_id, $order->orderid);
            } else {
                exception('开通会员失败');
            }
        }
    }
}
